package edu.xlh.travel.websocket.controller;

import com.alibaba.fastjson.JSON;
import edu.xlh.travel.pojo.User;
import edu.xlh.travel.websocket.domin.*;
import edu.xlh.travel.websocket.service.*;
import edu.xlh.travel.websocket.utils.PageTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/chatController")
public class WebScoketController {

   @Autowired
   private Emp_friendService emp_friendService;

   @Autowired
   private Emp_groupService emp_groupService;

   @Autowired
   private EmpInfoService empInfoService;

   @Autowired
   private FriendInfoService friendInfoService;

   @Autowired
   private GroupInfoService groupInfoService;

    @RequestMapping("/findChatInfo")
    @ResponseBody
    public ChatInfo findChatInfo(HttpSession session) {
        User user = (User) session.getAttribute("user");
        EmpInfo empInfo = null;
        EmpInfo mine = null;
        //一个，session获取
        FriendInfo friendInfoOwner = null;

        //多个，数据库
        List<FriendInfo> friendInfoList = null;

        //装分组
        ArrayList<FriendInfo> friendInfoArrayList = new ArrayList<>();

        //装群
        ArrayList<GroupInfo> groupInfoArrayList = new ArrayList<>();




        if (user != null) {
            String userId = user.getId();
            empInfo = empInfoService.findEmpInfoById(userId);
            empInfo.setStatus("online");
            mine = empInfo;
            //在哪个列表组，此处自己应该在尊贵的用户那一组，且该组只有用户自己
            List<Emp_friend> emp_friendList = emp_friendService.findEmp_friendByUserId(userId);
            ArrayList<EmpInfo> empInfoArrayList = new ArrayList<>();
            //遍历emp_friendList，查询尊贵的用户那一组
            FriendInfo virtualFriendInfoOwner = new FriendInfo();
            for (Emp_friend emp_friend : emp_friendList) {
                String friendGroupId = emp_friend.getFriendGroupId();
                virtualFriendInfoOwner = friendInfoService.findOneById(friendGroupId);
                if (virtualFriendInfoOwner != null && "尊贵的用户".equals(virtualFriendInfoOwner.getGroupname())) {
                    friendInfoOwner = virtualFriendInfoOwner;
                    empInfoArrayList.add(empInfo);
                    //尊贵的用户那一组只有用户自己
                    friendInfoOwner.setList(empInfoArrayList);
                    friendInfoArrayList.add(friendInfoOwner);
                }
            }
        //查找出每个用户默认存在的组，例如，客服组
         friendInfoList = friendInfoService.findAllExcludeOwner("尊贵的用户");
        //分组处理
        for (FriendInfo friendInfoExcludeOwner : friendInfoList) {
            ArrayList<EmpInfo> empInfoArrayList1 = new ArrayList<>();
            FriendInfo friendInfoExcludeOwner1 = new FriendInfo();
            //根据组id查找分组与用户关系表，获取相关的用户id
            List<Emp_friend> emp_friendListByGroupId = emp_friendService.findEmp_friendByGroupId(friendInfoExcludeOwner.getId().toString());
            //获取该组对应的人员，一对多
            for (Emp_friend emp_friend : emp_friendListByGroupId) {
                EmpInfo empInfoById = empInfoService.findEmpInfoById(emp_friend.getEmpId());
                empInfoById.setType("friend");
                empInfoArrayList1.add(empInfoById);
                String freindGroupId = emp_friend.getFriendGroupId();
                FriendInfo friendInfo = friendInfoService.findOneById(freindGroupId);
                friendInfoExcludeOwner1.setGroupname(friendInfo.getGroupname());
            }
            friendInfoExcludeOwner1.setList(empInfoArrayList1);
            friendInfoArrayList.add(friendInfoExcludeOwner1);

            //找出已登录用户所加的群
            //1.session中获取用户id，根据id获取用户-群表中所有的群
            List<Emp_group> emp_group = emp_groupService.findEmp_groupByUserId(userId);
            //2.遍历所有用户-群记录，获取群id，根据群id获取群信息，加入数组
            for (Emp_group empGroup : emp_group) {
                String groupId = empGroup.getGroupId();
                GroupInfo groupInfo = groupInfoService.findByGroupId(groupId);
                groupInfoArrayList.add(groupInfo);
            }
        }

        } else {
            //查找出每个用户默认存在的组，例如，客服组
            friendInfoList = friendInfoService.findAllExcludeOwner("尊贵的用户");
            //分组处理
            for (FriendInfo friendInfoExcludeOwner : friendInfoList) {
                ArrayList<EmpInfo> empInfoArrayList1 = new ArrayList<>();
                FriendInfo friendInfoExcludeOwner1 = new FriendInfo();
                friendInfoExcludeOwner1 = friendInfoExcludeOwner;
                //根据组id查找分组与用户关系表，获取相关的用户id
                List<Emp_friend> emp_friendListByGroupId = emp_friendService.findEmp_friendByGroupId(friendInfoExcludeOwner.getId().toString());
                //获取该组对应的人员，一对多
                for (Emp_friend emp_friend : emp_friendListByGroupId) {
                    EmpInfo empInfoById = empInfoService.findEmpInfoById(emp_friend.getEmpId());
                    empInfoById.setType("friend");
                    System.out.println(empInfoById.getUsername()+"11");
                    empInfoArrayList1.add(empInfoById);
                }
                friendInfoExcludeOwner1.setList(empInfoArrayList1);
                friendInfoArrayList.add(friendInfoExcludeOwner1);
            }

        }

        List<FriendInfo> friendInfos = friendInfoArrayList;

        ChatInfo chatInfo = new ChatInfo();
        chatInfo.setCode(0);
        chatInfo.setMsg("ok");
        Map map = chatInfo.getData();
        map.put("mine", mine);
        map.put("friend", friendInfos);
        List<GroupInfo> list = groupInfoArrayList;
        map.put("group", list);
        return chatInfo;
    }

    @RequestMapping("/getMembers")
    @ResponseBody
    public String getMembers(HttpSession session) {
        User user = (User) session.getAttribute("user");
        PageTable<Map<String, Object>> pageTable = new PageTable<Map<String, Object>>();
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        if (user != null) {
            String userId = user.getId();
            //查询当前用户所在群的所有人员
            //Map groupEmps = groupInfoService.findGroupEmps(id);
            ArrayList<EmpInfo> arrayList = new ArrayList();
            //根据用户id查询所有的群，用户-群表
            List<Emp_group> emp_groupByUserId = emp_groupService.findEmp_groupByUserId(userId);
            //遍历所有的记录，获取用户id，根据用户id获取用户，将用户加入到一个链表当中
            for (Emp_group emp_group : emp_groupByUserId) {
                //根据群id获取多个用户-群记录，根据用户id查询用户，加入链表
                    List<Emp_group> emp_groupByGroupId = emp_groupService.findByGroupId(emp_group.getGroupId());
                for (Emp_group empGroup : emp_groupByGroupId) {
                    String empId = empGroup.getEmpId();
                    if (!userId.equals(empId)) {
                        EmpInfo empInfoById = empInfoService.findEmpInfoById(empId);
                        arrayList.add(empInfoById);
                    }
                }
            }
            EmpInfo empInfo = empInfoService.findEmpInfoById(userId);
            empInfo.setStatus("online");
            System.out.println(empInfo.getUsername());
            stringObjectHashMap.put("owner" , empInfo);
            stringObjectHashMap.put("list" , arrayList);
        }
        //设置code
        pageTable.setCode(0);
        //设置msg
        pageTable.setMsg("获取成功");
        //设置data
        pageTable.setData(stringObjectHashMap);
        //转成字符串形式返回给前台
        return JSON.toJSONString(pageTable);
    }
   /* @Override
    public Map findGroupEmps(Integer groupId) {
        Map map = new HashMap();
        map.put("owner", "");
        //查询当前用户所在群成员的人数--value类型 Integer
        map.put("members", groupInfoDao.findGroupEmpsNum(groupId));
        //查询当前用户所在群的所有人员--value类型 List<EmpInfo>
        map.put("list", groupInfoDao.findGroupEmps(groupId));
        return map;
    }*/
}
